﻿'Namespace Hash
'    Public Class CRC32
'        Private Shared mCRC32Table(255) As Integer
'        ' This is the official polynomial used by CRC32 in PKZip.
'        ' Often the polynomial is shown reversed (04C11DB7).
'        Private Const dwPolynomial As Integer = &HEDB88320
'        Private Const BUFFER_SIZE As Integer = 1024

'        Shared Sub New()
'            Dim dwCrc As Integer

'            For i As Integer = 0 To 255
'                dwCrc = i
'                For j As Integer = 8 To 1 Step -1
'                    If CBool(dwCrc And 1) Then
'                        dwCrc = CInt(((dwCrc And &HFFFFFFFE) \ 2&) And &H7FFFFFFF)
'                        dwCrc = dwCrc Xor dwPolynomial
'                    Else
'                        dwCrc = CInt(((dwCrc And &HFFFFFFFE) \ 2&) And &H7FFFFFFF)
'                    End If
'                Next j
'                mCRC32Table(i) = dwCrc
'            Next i
'        End Sub

'        Public Shared Function Compute(ByVal data() As Byte) As Integer
'            Return Compute(data, 0, data.Count)
'        End Function


'        Public Shared Function Compute(ByVal data() As Byte, ByVal offset As Integer, ByVal count As Integer) As Integer
'            Dim crc32Result As Integer = &HFFFFFFFF

'            Dim iLookup As Integer
'            For i As Integer = offset To count - 1
'                iLookup = (crc32Result And &HFF) Xor data(i)
'                crc32Result = ((crc32Result And &HFFFFFF00) \ &H100) And &HFFFFFF   ' nasty shr 8 with vb :/
'                crc32Result = crc32Result Xor mCRC32Table(iLookup)
'            Next i
'            Return Not (crc32Result)
'        End Function
'        Public Shared Function Compute(ByVal stream As System.IO.Stream) As Integer
'            Dim crc32Result As Integer
'            crc32Result = &HFFFFFFFF

'            Dim buffer(BUFFER_SIZE - 1) As Byte
'            Dim readSize As Integer = BUFFER_SIZE

'            Dim count As Integer = stream.Read(buffer, 0, readSize)
'            Dim i As Integer
'            Dim iLookup As Integer
'            Do While (count > 0)
'                For i = 0 To count - 1
'                    iLookup = (crc32Result And &HFF) Xor buffer(i)
'                    crc32Result = ((crc32Result And &HFFFFFF00) \ &H100) And &HFFFFFF   ' nasty shr 8 with vb :/
'                    crc32Result = crc32Result Xor mCRC32Table(iLookup)
'                Next i
'                count = stream.Read(buffer, 0, readSize)
'            Loop

'            Return Not (crc32Result)

'        End Function
'    End Class
'End Namespace